/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.produto;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.produto.FonteInterface;
import br.gov.cemaden.library.model.produto.Fonte;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class FonteDao extends GenericDao implements FonteInterface {

    private Session session;

    public FonteDao(Session session) {
        this.session = session;
    }

    @Override
    public List<Fonte> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Fonte> fonte = null;
        try {
            Query query = session.getNamedQuery("Fonte.findAll");
            fonte = (List<Fonte>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no FonteDao.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return fonte;
    }

    @Override
    public Fonte findByIdFonte(Long idFonte) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Fonte fonte = null;
        try {
            Query query = session.getNamedQuery("Fonte.findByIdFonte").setParameter("idFonte", idFonte);
            fonte = (Fonte) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no FonteDao.findByIdFonte: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return fonte;
    }

    @Override
    public List<Fonte> findByDescricao(String descricao) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Fonte> fonte = null;
        try {
            Query query = session.getNamedQuery("Fonte.findByDescricao").setParameter("descricao", descricao);
            fonte = (List<Fonte>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no FonteDao.findByDescricao: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return fonte;
    }

    @Override
    public List<Fonte> findByLatitude(Double latitude) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Fonte> fonte = null;
        try {
            Query query = session.getNamedQuery("Fonte.findByLatitude").setParameter("latitude", latitude);
            fonte = (List<Fonte>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no FonteDao.findByLatitude: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return fonte;
    }

    @Override
    public List<Fonte> findByLongitude(Double longitude) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Fonte> fonte = null;
        try {
            Query query = session.getNamedQuery("Fonte.findByLongitude").setParameter("longitude", longitude);
            fonte = (List<Fonte>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no FonteDao.findByLongitude: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return fonte;
    }

    @Override
    public List<Fonte> findByNome(String nome) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Fonte> fonte = null;
        try {
            Query query = session.getNamedQuery("Fonte.findByNome").setParameter("nome", nome);
            fonte = (List<Fonte>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no FonteDao.findByNome: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return fonte;
    }

    @Override
    public void insert(Fonte fonte) {
        try {
            saveOrUpdate(this.session, fonte);
        } catch (Exception ex) {
            Logger.getLogger(ProdutoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Fonte fonte) {
        try {
            saveOrUpdate(this.session, fonte);
        } catch (Exception ex) {
            Logger.getLogger(ProdutoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Fonte fonte) {
        try {
            remove(this.session, fonte);
        } catch (Exception ex) {
            Logger.getLogger(ProdutoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
